﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceMan
{

    public class C_Drawing
    {
        Texture2D texture2D, GlowMask;
        Rectangle sourceRecangle, destinationRectangle;
        int length, currentFrame;
        float frameTime, counter;
        bool isAnimated, isLooped;
        Dictionary<string, AnimationStructure> animationDictionary;
        Vector2 origin;
        float rotation;
        bool AlwaysDraw, glowUpdate;
        Color color, glowColor;

        public C_Drawing(Texture2D texture2D, Texture2D GlowMask, Dictionary<string, AnimationStructure> animationDictionary)
        {
            
            this.texture2D = texture2D;
            this.GlowMask = GlowMask;
            this.animationDictionary = animationDictionary;
            color = Color.White;
            if (animationDictionary.ContainsKey("ref"))
            {
                destinationRectangle.Width = animationDictionary["ref"].width;
                destinationRectangle.Height = animationDictionary["ref"].height;
                sourceRecangle.Y = animationDictionary["ref"].startPosition * animationDictionary["ref"].height;
                sourceRecangle.X = 0;
                sourceRecangle.Width = animationDictionary["ref"].width;
                sourceRecangle.Height = animationDictionary["ref"].height;
                this.isAnimated = animationDictionary["ref"].animated;
                this.isLooped = animationDictionary["ref"].loop;
                this.frameTime = animationDictionary["ref"].frameTime;
                length = animationDictionary["ref"].length;
                origin = animationDictionary["ref"].origin;
                counter = frameTime;
            }
            else
            {
                AnimationStructure animation = animationDictionary.Values.ElementAt(0);
                destinationRectangle.Width = animation.width;
                destinationRectangle.Height = animation.height;
                sourceRecangle.Y = animation.startPosition * animation.height;
                sourceRecangle.X = 0;
                sourceRecangle.Width = animation.width;
                sourceRecangle.Height = animation.height;
                this.isAnimated = animation.animated;
                this.isLooped = animation.loop;
                this.frameTime = animation.frameTime;
                length = animation.length;
                origin = animation.origin;
                counter = frameTime;
            }
        }

        public void UpdateGLow(Color color)
        {
            if (GlowMask != null)
            {
                glowColor = color;
                glowUpdate = true;
            }
        }

        public void Update(Vector2 position, float rotation, int? Width, int? Height, bool AlwaysDraw, Color? Color)
        {
            this.AlwaysDraw = AlwaysDraw;
            this.rotation = rotation;
            destinationRectangle.X = (int)position.X;
            destinationRectangle.Y = (int)position.Y;
            if (Width != null)
            {
                destinationRectangle.Width = (int)Width;
            }
            if (Height != null)
            {
                destinationRectangle.Height = (int)Height;
            }

            if (Color != null)
            {
                color = (Color)Color;
                
            }

            if (isAnimated)
            {
                counter -= S_Global.getTimescale();
                if (counter < 0)
                {
                    currentFrame++;
                    sourceRecangle.X = currentFrame * sourceRecangle.Width;
                    counter = frameTime;

                    if (currentFrame > length)
                    {
                        if (!isLooped)
                            isAnimated = false;
                        else
                            currentFrame = 0;
                    }
                }
            }
        }

        public Rectangle GetCurrentRect()
        {
            return destinationRectangle;
        }

        public void Draw(SpriteBatch spriteBatch, DrawMode drawMode, Rectangle DrawSpace)
        {
            Color _glow;
            if (glowUpdate)
            {
                _glow = glowColor;
            }
            else
            {
                _glow = color;
            }
            if (destinationRectangle.Intersects(DrawSpace))
            {
                if (drawMode == DrawMode.Texture)
                {
                    spriteBatch.Draw(texture2D, destinationRectangle, sourceRecangle, color, rotation, origin, SpriteEffects.None, 0f);
                }
                else
                {
                    spriteBatch.Draw(texture2D, destinationRectangle, sourceRecangle, Color.Black, rotation, origin, SpriteEffects.None, 0f);
                    if (drawMode == DrawMode.Glow && GlowMask != null)
                    {
                        spriteBatch.Draw(GlowMask, destinationRectangle, sourceRecangle, _glow, rotation, origin, SpriteEffects.None, 0f);
                    }
                }
            }
            glowUpdate = false;
        }

        public void setAnimation(string animationID)
        {
            if(animationDictionary.ContainsKey(animationID))
            {
                AnimationStructure animation = animationDictionary[animationID];
                sourceRecangle.Y = animation.startPosition * animation.height;
                sourceRecangle.X = 0;
                sourceRecangle.Width = animation.width;
                sourceRecangle.Height = animation.height;
                this.isAnimated = animation.animated;
                this.isLooped = animation.loop;
                this.frameTime = animation.frameTime;
                length = animation.length;
                origin = animation.origin;
                counter = frameTime;
            }
        }
    }
}
